package day27;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {
    public static void main(String[] args) throws IOException {
        //1.创建服务端对象
        ServerSocket server=new ServerSocket(9999);
        while (true){
            //2.等待客户端连接
            Socket client=server.accept();
            System.out.println(client.getInetAddress()+"连接成功");
            //3.获取输入输出流
            InputStream is=client.getInputStream(); //负责读取客户端写入的数据
            OutputStream os=client.getOutputStream(); //负责向客户端写入数据
            os.write("你好,我是服务端\n".getBytes());

            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            String result=br.readLine(); //BUG 读取一行,写数据时一定要加换行，否则就不知道什么时候结束
            System.out.println(client.getInetAddress()+"客户端:"+result);
        }

    }
}
